/*
 * Copyright (C) 2017 WordPlat Open Source Project
 *
 *      https://wordplat.com/InteractiveKLineView/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.ikvstockchart.drawing;


import com.wordplat.ikvstockchart.entry.EntrySet;
import com.wordplat.ikvstockchart.entry.SizeColor;
import com.wordplat.ikvstockchart.render.AbstractRender;
import com.wordplat.ikvstockchart.utils.StringUtils;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;

import java.text.DecimalFormat;

/**
 * <p>TimeLineGridAxisDrawing</p>
 * <p>Date: 2017/3/10</p>
 *
 * @author afon
 * @since 2021-05-09
 */
public class TimeLineGridAxisDrawing implements IDrawing {
    /**
     * X 轴标签的画笔
     */
    private Paint xLabelPaint;
    /**
     * Y 轴标签的画笔
     */
    private Paint yLabelPaint;
    /**
     * X 轴和 Y 轴的画笔
     */
    private Paint axisPaint;
    /**
     * k线图网格线画笔
     */
    private Paint gridPaint;
    /**
     * 用于 labelPaint 计算文字位置
     */
    private final Paint.FontMetrics fontMetrics = new Paint.FontMetrics(0, 0, 0, 0, 0);
    private final DecimalFormat decimalFormatter = new DecimalFormat("0.00");
    /**
     * 分时图显示区域
     */
    private final RectFloat chartRect = new RectFloat();
    private AbstractRender render;
    private SizeColor sizeColor;

    private final float[] pointCache = new float[2];
    private final float[] valueCache = new float[5];
    private float lineHeight;
    private float lineWidth;

    private int entrySetSize;

    @Override
    public void onInit(RectFloat contentRect, AbstractRender render) {
        this.render = render;
        this.sizeColor = render.getSizeColor();

        if (xLabelPaint == null) {
            xLabelPaint = new Paint();
            xLabelPaint.setAntiAlias(true);
        }
        xLabelPaint.setTextSize(StringUtils.floatToInt(sizeColor.getXLabelSize()));
        xLabelPaint.setColor(new Color(sizeColor.getXLabelColor()));
        xLabelPaint.getFontMetrics();

        if (yLabelPaint == null) {
            yLabelPaint = new Paint();
            yLabelPaint.setAntiAlias(true);
        }
        yLabelPaint.setTextSize(StringUtils.floatToInt(sizeColor.getYLabelSize()));

        if (axisPaint == null) {
            axisPaint = new Paint();
            axisPaint.setAntiAlias(true);
            axisPaint.setStyle(Paint.Style.STROKE_STYLE);
        }

        if (gridPaint == null) {
            gridPaint = new Paint();
            gridPaint.setAntiAlias(true);
            gridPaint.setStyle(Paint.Style.STROKE_STYLE);
        }

        axisPaint.setStrokeWidth(sizeColor.getAxisSize());
        axisPaint.setColor(new Color(sizeColor.getAxisColor()));

        gridPaint.setStrokeWidth(sizeColor.getGridSize());
        gridPaint.setColor(new Color(sizeColor.getGridColor()));

        chartRect.left = contentRect.left;
        chartRect.top = contentRect.top;
        chartRect.bottom = contentRect.bottom;
        chartRect.right = contentRect.right;

        lineHeight = chartRect.getHeight() / 4;
        lineWidth = chartRect.getWidth() / 4;
    }

    @Override
    public void computePoint(int minIndex, int maxIndex, int currentIndex) {

    }

    @Override
    public void onComputeOver(Canvas canvas, int minIndex, int maxIndex, float minY, float maxY) {
        final EntrySet entrySet = render.getEntrySet();
        entrySetSize = entrySet.getEntryList().size();
        /**
         * 绘制 最外层大框框
         */
        canvas.drawRect(chartRect, axisPaint);

        /**
         * 绘制 三条横向网格线
         */
        for (int i = 0; i < 3; i++) {
            float lineTop = chartRect.top + (i + 1) * lineHeight;
            canvas.drawLine(chartRect.left, lineTop, chartRect.right, lineTop, gridPaint);
        }

        for (int i = 0; i < 5; i++) {
            float lineLeft = chartRect.left + i * lineWidth;

            if (i != 0 && i != 4) {
                /**
                 *  绘制 三条竖向网格线
                 */
                canvas.drawLine(lineLeft, chartRect.top, lineLeft, chartRect.bottom, gridPaint);
                xLabelPaint.setTextAlign(TextAlignment.CENTER);
            } else if (i == 0) {
                xLabelPaint.setTextAlign(TextAlignment.LEFT);
            } else {
                xLabelPaint.setTextAlign(TextAlignment.RIGHT);
            }

            /**
             * 绘制 X 轴 label
             */
            if (entrySetSize > 0) {
                canvas.drawText(xLabelPaint,
                        entrySet.getEntryList().get(i).getXLabel(),
                        lineLeft,
                        chartRect.bottom + render.getSizeColor().getXLabelSize());
            }
        }
    }

    @Override
    public void onDrawOver(Canvas canvas) {
        if (entrySetSize < 1) {
            return;
        }
        /**
         * 绘制 Y 轴左边 label
         */
        for (int i = 0; i < 5; i++) {
            float lineTop = chartRect.top + i * lineHeight;
            pointCache[1] = lineTop;
            render.invertMapPoints(pointCache);
            String value = decimalFormatter.format(pointCache[1]);

            if (i == 0) {
                pointCache[0] = lineTop - fontMetrics.top;
            } else if (i == 4) {
                pointCache[0] = lineTop - fontMetrics.bottom;
            } else {
                pointCache[0] = lineTop + fontMetrics.bottom;
            }

            if (i == 2) {
                yLabelPaint.setColor(new Color(sizeColor.getNeutralColor()));
            } else if (i > 2) {
                yLabelPaint.setColor(new Color(sizeColor.getNeutralColor()));
            } else {
                yLabelPaint.setColor(new Color(sizeColor.getIncreasingColor()));
            }
            yLabelPaint.setTextAlign(TextAlignment.LEFT);

            canvas.drawText(yLabelPaint, value, chartRect.left + 5, pointCache[0]);

            valueCache[i] = pointCache[1];
        }

        /**
         * 绘制 Y 轴右边 label
         */
        for (int i = 0; i < 5; i++) {
            float percent = (valueCache[i] - valueCache[2]) / valueCache[2] * 100;
            String value = decimalFormatter.format(percent);
            float lineTop = chartRect.top + i * lineHeight;

            if (i == 0) {
                pointCache[0] = lineTop - fontMetrics.top;
            } else if (i == 4) {
                pointCache[0] = lineTop - fontMetrics.bottom;
            } else {
                pointCache[0] = lineTop + fontMetrics.bottom;
            }

            if (i == 2) {
                yLabelPaint.setColor(new Color(sizeColor.getNeutralColor()));
            } else if (i > 2) {
                yLabelPaint.setColor(new Color(sizeColor.getDecreasingColor()));
            } else {
                yLabelPaint.setColor(new Color(sizeColor.getIncreasingColor()));
            }
            yLabelPaint.setTextAlign(TextAlignment.RIGHT);

            canvas.drawText(yLabelPaint, value + "%", chartRect.right - 5, pointCache[0]);
        }
    }
}
